var dragflag = 0;
var draggedobj = null;
var dragtype = "";
var drag_obj_start_x = null;
var drag_obj_start_y = null;
var drag_mouse_start_x = null;
var drag_mouse_start_y = null;

function stopDrag () {
	if ( dragflag-- == 1 ) draggedobj = null;
}

function startDrag ( event, obj, type ) {
	draggedobj = obj;
	dragtype = type;
	dragflag = 2;
	drag_mouse_start_x = event.clientX;
	drag_mouse_start_y = event.clientY;
	if ( dragtype == "move" ) {
		drag_obj_start_x = parseInt(draggedobj.style.left);
		drag_obj_start_y = parseInt(draggedobj.style.top);
	} else if ( dragtype == "resize" ) {
		drag_obj_start_x = parseInt(draggedobj.style.width);
		drag_obj_start_y = parseInt(draggedobj.style.height);
	}
}

function switchDrag ( event, obj, type ) {
	if ( draggedobj == null ) {
		startDrag ( event, obj, type );
	} else {
		stopDrag();
	}
}

function drag ( event ) {
	if ( draggedobj ) {
		if ( dragtype == "move" ) {
			draggedobj.style.left = drag_obj_start_x + (event.clientX - drag_mouse_start_x);
			draggedobj.style.top = drag_obj_start_y + (event.clientY - drag_mouse_start_y);
		} else if ( dragtype == "resize" ) {
			draggedobj.style.width = drag_obj_start_x + (event.clientX - drag_mouse_start_x);
			draggedobj.style.height = drag_obj_start_y + (event.clientY - drag_mouse_start_y);
		}
	}	
}

function newResize ( obj ) {
	var resize=document.createElement('div');
	resize.setAttribute('id', obj.id+'_resize');
	resize.setAttribute('class', 'resize_div');
	resize.onclick = function ( event ) {
		switchDrag(event,obj,"resize");
	}
	return resize;
}

function newMove ( obj ) {
	var move=document.createElement('div');
	move.setAttribute('id', obj.id+'_move');
	move.setAttribute('class', 'move_div');
	move.onclick = function ( event ) {
		switchDrag(event,obj,"move");
	}
	return move;
}

